<?php
require_once 'pdo.class.php';
$host = '192.168.50.67';
$dbname = 'dim5_iot';
$userName = 'root';
$password = '123456';
$device_product_ids = [
    ["K1adoeNvgJyI", "aMEgdtuXlB68"],
    ["K1Ae7ifSUYQ6", "qOMyoKdc3flG"],
];
for ($i = 0; $i < 2; $i++) {
    $data[] = [
        'id' => generateRandomCode(20),
        'name' => generateRandomCode(5),
        'org_id' => 2,
        'owner' => 'doumingquan',
        'device_id' => $device_product_ids[rand(0, 1)][0],
        'product_id' => $device_product_ids[rand(0, 1)][1],
        'type' => '高温预警',
        'level' => 'MAJOR',
        'state' => '0',
        'details' => '{"humidity":43.6,"temperature":18.6,"ts":"2024-05-10 11:16:38.617"}',
        'time' => date('Y-m-d H:i:s')
    ];
}
//var_dump($data);die;

try {
    $db = new Database($host,$dbname, $userName, $password);
    var_dump($db);
    $db->beginTransaction();
    // 执行一些数据库操作
    $db->batchInsert('device_alarms', $data);
    $db->commit();
    echo "插入完成！";
} catch (Exception $e) {
    $db->rollBack();
    // 处理异常
    echo "Error: " . $e->getMessage();
}
function generateRandomCode($length = 12)
{
    $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $code .= 'abcdefghijklmnopqrstuvwxyz';
    $code .= '0123456789';

    // 将字符串打乱，创建一个包含所有可能字符的数组
    $shuffledCode = str_shuffle($code);

    // 从打乱后的字符串中取出前$length位字符作为随机码
    $randomCode = substr($shuffledCode, 0, $length);

    return $randomCode;
}

